package com.cmict.data.bpm.framework.flowable.core.behavior.script.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmict.data.bpm.dal.dataobject.definition.BpmTaskAssignRuleDO;
import com.cmict.data.bpm.framework.flowable.core.behavior.script.BpmTaskAssignScript;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import org.flowable.engine.delegate.DelegateExecution;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分配流程变量对应的部门和用户为流程审批人
 *
 * @author liutao
 * @date 2022/11/8
 */
public abstract class BpmTaskAssignVariableAbstractScript implements BpmTaskAssignScript {

    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private DeptApi deptApi;

    /**
     * 部门负责人作为流程审批人
     */
    protected Set<Long> calculateTaskCandidateDeptHead(DelegateExecution execution, BpmTaskAssignRuleDO rule) {
        Set<Long> userSet = Collections.emptySet();
        String variableName = rule.getVariableName();
        String deptId =String.valueOf(execution.getVariable(variableName));
        if (deptId != null) {
            return calculateUserByDeptInfo(deptId);
        }
        return userSet;
    }

    protected Set<Long> calculateTaskCandidateUser(DelegateExecution execution, BpmTaskAssignRuleDO rule) {
        String variableName = rule.getVariableName();
        String user = String.valueOf(execution.getVariable(variableName));
        if (user != null) {
            return calculateUserByUserInfo(user);
        }
        return Collections.emptySet();
    }

    private Set<Long> calculateUserByDeptInfo(String dept) {
        List<String> deptList = convertData(dept);
        if (CollectionUtil.isNotEmpty(deptList)) {
            List<DeptRespDTO> deptS = deptList.parallelStream()
                    .map(item -> deptApi.getDeptByName(item).getCheckedData()).filter(item->item!=null).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(deptS)) {
                List<Long> deptIdList = deptList.stream().map(item -> Long.parseLong(item)).collect(Collectors.toList());
                deptS = deptApi.getDepts(deptIdList).getCheckedData();
            }
            return deptS.stream().map(item -> item.getLeaderUserId()).collect(Collectors.toSet());
        }
        return Collections.emptySet();
    }
    private Set<Long> calculateUserByUserInfo(String user) {
        List<String> userList = convertData(user);
        if (CollectionUtil.isNotEmpty(userList)) {
            List<AdminUserRespDTO> users = adminUserApi.getUsersByUserNames(userList).getCheckedData();
            if (CollectionUtil.isEmpty(users)) {
                List<Long> userIdList = userList.stream().map(item -> Long.parseLong(item)).collect(Collectors.toList());
                users = adminUserApi.getUsers(userIdList).getCheckedData();
            }
            return users.stream().map(item -> item.getId()).collect(Collectors.toSet());
        }
        return Collections.emptySet();
    }

    private List<String>  convertData(String data) {
        if (data.startsWith("[") && data.endsWith("]")) {
            data = data.substring(1, data.length() - 1);
        }
        String demoArr[] = data.split(",");
        List<String> dataList = Arrays.asList(demoArr);
        return dataList;
    }
}
